package com.zzzy.doc.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzzy.common.vo.PageResult;
import com.zzzy.doc.common.enums.DocNotifyMessageModuleEnum;
import com.zzzy.doc.dao.entity.DocNotifyMessage;
import com.zzzy.doc.dao.entity.Document;
import com.zzzy.doc.dao.mapper.DocNotifyMessageMapper;
import com.zzzy.doc.service.exchange.DocReceiveRecordService;
import com.zzzy.doc.vo.notify.DocNotifyMessagePageReq;
import com.zzzy.framework.common.enums.common.NotifyMessageStatusEnum;
import com.zzzy.framework.common.util.SecurityUtils;
import com.zzzy.framework.core.mybatis.query.LambdaQueryWrapperE;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 公文提醒消息
 *
 * @author fc
 * @since 2024-09-04
 */
@Service
public class DocNotifyMessageService extends ServiceImpl<DocNotifyMessageMapper, DocNotifyMessage> {

    @Resource
    private DocService docService;
    @Resource
    private DocReceiveRecordService receiveRecordService;

    public PageResult<DocNotifyMessage> getDocNotifyMessagePage(DocNotifyMessagePageReq queryReq) {
        LambdaQueryWrapperE<DocNotifyMessage> queryWrapperE = new LambdaQueryWrapperE<>();
        queryWrapperE.eq(DocNotifyMessage::getUserId, SecurityUtils.getLoginUser().getId())
                .likeIfPresent(DocNotifyMessage::getMessage, queryReq.getMessage())
                .eqIfPresent(DocNotifyMessage::getStatus, queryReq.getStatus())
                .betweenIfPresent(DocNotifyMessage::getCreateTime, queryReq.getBeginTime(), Objects.nonNull(queryReq.getEndTime()) ? queryReq.getEndTime().plusDays(1) : null)
                .orderByDesc(DocNotifyMessage::getId);
        return baseMapper.selectPage(queryReq, queryWrapperE);
    }

    public void markNotifyMessageRead(Long[] messageIds) {
        if (messageIds.length == 0){
            return;
        }
        Long userId = SecurityUtils.getLoginUser().getId();
        baseMapper.update(new LambdaUpdateWrapper<DocNotifyMessage>().eq(DocNotifyMessage::getUserId, userId).in(DocNotifyMessage::getId, messageIds)
                .set(DocNotifyMessage::getStatus, NotifyMessageStatusEnum.READ.getValue()));
    }

    public void addNotifyMessage(Long docId, Long userId, DocNotifyMessageModuleEnum moduleEnum, String message){
        DocNotifyMessage notifyMessage = buildNotifyMessage(docId, userId, moduleEnum, message);
        baseMapper.insert(notifyMessage);
    }

    public void addNotifyMessage(Long docId, List<Long> userIdList, DocNotifyMessageModuleEnum moduleEnum, String message){
        userIdList.forEach(userId -> addNotifyMessage(docId, userId, moduleEnum, message));
    }

    private DocNotifyMessage buildNotifyMessage(Long docId, Long userId, DocNotifyMessageModuleEnum moduleEnum, String message){
        DocNotifyMessage notifyMessage = new DocNotifyMessage();
        notifyMessage.setSourceId(docId);
        notifyMessage.setUserId(userId);
        notifyMessage.setModule(moduleEnum.getValue());
        notifyMessage.setMessage(message);
        notifyMessage.setCreateTime(LocalDateTime.now());
        notifyMessage.setStatus(NotifyMessageStatusEnum.NOT_READ.getValue());
        return notifyMessage;
    }

    public Document getDocByMessageId(Long id) {
        DocNotifyMessage docNotifyMessage = baseMapper.selectById(id);
        if (DocNotifyMessageModuleEnum.EXCHANGE_RECEIVE.getValue().equals(docNotifyMessage.getModule())){
            return receiveRecordService.getDocBasicInfo(docNotifyMessage.getSourceId());
        } else {
            return docService.getDocById(docNotifyMessage.getSourceId());
        }
    }
}
